package com.becca.cake.core.excel.excelWriter.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.becca.cake.core.excel.excelReader.DownLoadFile;
import com.becca.cake.core.excel.excelWriter.BaseExcelHandle;
import com.becca.cake.core.excel.excelWriter.ExcelInfo;

public class ExcelWriteHandle extends AbsExcelWrite {
    private static Log logger = LogFactory.getLog(ExcelWriteHandle.class);

    private static class excelHandle {
        private static BaseExcelHandle INSTACE = new ExcelWriteHandle();
    }

    public static BaseExcelHandle getInstance() {
        return ExcelWriteHandle.excelHandle.INSTACE;
    }

    public void writeEcxcel(ExcelInfo info, HttpServletRequest request, HttpServletResponse response) {
        try {
            File foder = new File(getExcelFoderPath());
            if (!foder.exists()) {
                if (!foder.mkdirs()) {
                    if (logger.isErrorEnabled()) {
                        logger.error("创建Excel导出临时存放文件夹失败!");
                    }
                }
            }
            //Excel导出文件
            File f = new File(getExcelFoderPath() + File.separator + info.getFileName() + ".xls");
            //生成Excel
            super.handleExcel(new FileOutputStream(f), info);
            //下载Excel到桌面并删除原Excel
            DownLoadFile.exportFile(response, request, f, true);
        } catch (WriteException e) {
            logger.error("导出Excel异常!", e);
        } catch (FileNotFoundException e) {
            logger.error("Excel未知异常错误!", e);
        } catch (IOException e) {
            logger.error("Excel导出是IO流异常!", e);
        }
    }

    public static void main(String[] args) {
        ExcelInfo info = new ExcelInfo();
        info.setColumnHeader("拍品编号,拍品名,详细描述,分类,制作费,保险额,状态,退拍原因,退拍备注,拍品备注");// auctionCharacterId
        info.setFeilds("auctionNo,name,descr,initialPrice,auctionCategoryId,onloadFee,insurePrice,status,returnReason,returnRemarks,remarks");
        info.setDatas(null);
        info.setSheetName("拍品信息");
        info.setFileName("Excel文件名称");
        //        ExcelWriteHandle.getInstance().writeEcxcel(info);
    }
}
